package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.RoleMapper;
import com.kkb.mapper.UserMapper;
import com.kkb.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    /**
     * 登录
     */
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;

    public User login(String userName, String password) {
        UserExample adminExample = new UserExample();
        UserExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUserLoginNameEqualTo(userName);
        criteria.andUserPasswordEqualTo(password);
        criteria.andIsDelEqualTo(0);//0正常   1删除   没有被删除的才能登录
        List<User> userList = userMapper.selectByExample(adminExample);
        if (userList.size() == 1) {
            User user = userList.get(0);
            Integer roleId = user.getRoleId();
            Role role = roleMapper.queryById(roleId);
            if (role != null && role.getRoleState() == 0)
                user.setRole(role);
            return user;
        }
        return null;
    }

    /**
     * 分页查询
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<User> queryByPage(Integer pageNum, Integer pageSize, String userName) {
        User user = new User();
        user.setUserLoginName(userName);
        user.setIsDel(0);
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.queryByPage(user);
        return new PageInfo<>(userList, 5);
    }

    /**
     * 主键查询
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public User queryById(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        Role role = roleMapper.selectByPrimaryKey(user.getRoleId());
        user.setRole(role);
        return user;
    }

    /**
     * 登录名重复性验证
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<User> queryByUserLoginName(String userLoginName) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserLoginNameEqualTo(userLoginName);
        List<User> userList = userMapper.selectByExample(userExample);
        return userList;
    }

    /**
     * 添加用户
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addUser(User user) {
        user.setIsDel(0);
        return userMapper.insertSelective(user);
    }

    /**
     * 更新
     *
     * @param user
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateUser(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserLoginNameEqualTo(user.getUserLoginName());
        criteria.andUserIdNotEqualTo(user.getUserId());
        List<User> userList = userMapper.selectByExample(userExample);
        if (userList.size() > 0) {
            return -1;
        }
        return userMapper.updateByPrimaryKeySelective(user);
    }


    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteUsers(List<Integer> userIds) {
        int i = userMapper.deleteUsers(userIds);
        return i;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteUser(Integer userId) {
        User user = new User();
        user.setUserId(userId);
        user.setIsDel(1);
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
